﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Data.SystemManager
{
    public class TechnologyManageRepository : RepositoryBase<TechnologyManage>
    {
        public IQueryable<ProductTechnologyView> GetList(Guid productId, string productName, string productNum)
        {
            var result = from tm in dbcontext.TechnologyManages
                         join pro in dbcontext.Products
                         on tm.MaterialId equals pro.Id
                         where (tm.ProductId == productId)
                          && (productName == "" || pro.Name.Contains(productName))
                          && (productNum == "" || pro.Number.Contains(productNum))
                         select new ProductTechnologyView
                         {
                             Id = tm.Id,
                             MaterialId = tm.MaterialId,
                             ProductId = tm.ProductId,
                             ProductName = pro.Name,
                             ProductNumber = pro.Number,
                             CreateTime = pro.CreateTime,
                             Status = pro.Status,
                             CreatorName = pro.CreatorName,
                             Price = pro.Price,
                         };
            return result;
        }

        /// <summary>
        /// 编辑根据id获取产品下的原材料
        /// </summary>
        /// <param name="pId"></param>
        /// <param name="tmId"></param>
        /// <returns></returns>
        public TechnologyManageView GetSingle(Guid pId, Guid tmId)
        {
            var result = from pro in dbcontext.Products
                         join tm in dbcontext.TechnologyManages
                         on pro.Id equals tm.MaterialId
                         where (tm.MaterialId == tmId)
                         && (tm.ProductId == pId)
                         select new TechnologyManageView
                         {
                             Id = tm.Id,
                             MaterialId = tm.MaterialId,
                             Status = tm.Status,
                             Amount = tm.Amount,
                             Ch = tm.Ch,
                             Cv = tm.Cv,
                             Tj = tm.Tj,
                             GiveH = tm.GiveH,
                             GiveW = tm.GiveW,
                             CarH = tm.CarH,
                             Equipment = tm.Equipment,
                             CreateTime = tm.CreateTime,
                             Memo = tm.Memo,
                             EquipmentType = tm.EquipmentType,
                             TechType = tm.TechType,

                             ProductId = tm.ProductId,
                             ProductName = pro.Name,
                             ProductNumber = pro.Number,
                             PCreateTime = pro.CreateTime,
                             ProductStatus = pro.Status,
                             //ProductType = pro.Type,
                             PCreatorName = pro.CreatorName,
                             ProductPrice = pro.Price,
                         };
            return result.FirstOrDefault();
        }

        /// <summary>
        /// 编辑根据id获取产品下的原材料
        /// </summary>
        /// <param name="pId"></param>
        /// <param name="tmId"></param>
        /// <returns></returns>
        public TechnologyManageView GetSingle(Guid pId)
        {
            var result = from pro in dbcontext.Products
                         join tm in dbcontext.TechnologyManages
                         on pro.Id equals tm.MaterialId
                         where (tm.ProductId == pId)
                         //&& pro.Type == 2
                         select new TechnologyManageView
                         {
                             Id = tm.Id,
                             MaterialId = tm.MaterialId,
                             Status = tm.Status,
                             Amount = tm.Amount,
                             Ch = tm.Ch,
                             Cv = tm.Cv,
                             Tj = tm.Tj,
                             GiveH = tm.GiveH,
                             GiveW = tm.GiveW,
                             CarH = tm.CarH,
                             Equipment = tm.Equipment,
                             CreateTime = tm.CreateTime,
                             Memo = tm.Memo,
                             EquipmentType=tm.EquipmentType,
                             TechType=tm.TechType,
                             Data=tm.Data,
                             MaterialIdsData = tm.MaterialIdsData,

                             ProductName = pro.Name,
                             //ProductId = tm.ProductId,
                             //ProductNumber = pro.Number,
                             //PCreateTime = pro.CreateTime,
                             //ProductStatus = pro.Status,
                             //ProductType = pro.Type,
                             //PCreatorName = pro.CreatorName,
                             //ProductPrice = pro.Price,

                         };
            return result.FirstOrDefault();
        }
    }
}
